package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.dto.SetmealDto;
import com.zrrd.reggie.common.CustException;
import com.zrrd.reggie.mapper.SetmealMapper;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveSetmeanWithDish(SetmealDto dto) {
        this.save(dto);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getSetmealWithDish(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    //修改
    @Override
    @Transactional
    public boolean updateSetmealWithDish(SetmealDto dto) {
        boolean a = this.updateById(dto);
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", dto.getId());
        boolean b = setmealDishService.remove(wrapper);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        boolean c = setmealDishService.saveBatch(setmealDishes);
        return a && b && c;
    }

    //删除
    @SneakyThrows
    @Override
    @Transactional
    public boolean deleteSetmeals(Long[] ids) {
        List<Long> idList = new ArrayList<>();
        //转为集合，顺便遍历查看是否有正在售卖的套餐
        for (Long id : ids) {
            if (this.getById(id).getStatus() == 0) {
                idList.add(id);
            } else {
                throw new CustException("不能删除正在售卖的商品");
            }
        }
        boolean a = this.removeByIds(idList);
        boolean b = true;
        for (Long id : idList) {
            QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
            wrapper.eq("setmeal_id", id);
            b = b && setmealDishService.remove(wrapper);
        }
        return a && b;
    }
}
